using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using SerialTool.Models;
using System;
using System.Threading.Tasks;

namespace SerialTool.ViewModels;

public partial class PresetCommandViewModel : ViewModelBase
{
    public PresetCommand Preset { get; }
    private readonly MainWindowViewModel _mainViewModel; // Reference to main VM for actions

    // Expose properties from Preset for easy binding
    public string Name => Preset.Name;
    public string CommandString => Preset.CommandString;
    public string EncodingType => Preset.EncodingType; // 暴露编码类型

    public PresetCommandViewModel(PresetCommand preset, MainWindowViewModel mainViewModel)
    {
        Preset = preset ?? throw new ArgumentNullException(nameof(preset));
        _mainViewModel = mainViewModel ?? throw new ArgumentNullException(nameof(mainViewModel));
    }

    // Command to send this specific preset
    [RelayCommand]
    private async Task Send()
    {
        // 1. Copy command text to main area for visibility
        _mainViewModel.CopyPresetCommandToMain(CommandString);
        // 2. Send the command directly using its own encoding
        await _mainViewModel.SendPresetDirectly(CommandString, EncodingType);
    }

    // Command to delete this specific preset
    [RelayCommand]
    private async Task Delete()
    {
        // Ask the main VM to delete this item's ViewModel
        await _mainViewModel.DeletePreset(this);
    }
} 